package sort;

import java.util.Arrays;

public class BubbleSort implements MySort{
    public static void main(String[] args) {
        //int a[] = {1,78,35,29,156,88,66};
        int a[] = {1,2,3,4,5,6,7};
        bubbleSort(a);
        System.out.println(Arrays.toString(a));
    }

    private static void bubbleSort(int[] a) {
        int count = a.length-1;
        int temp;
        int c = 0;
        boolean exchanged = false;
        for(int i=count;i>0;i--){
            exchanged = false;
            for (int j=0;j<i;j++){
                if(a[j]>a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    exchanged = true;
                }
                c++;
            }
            if(!exchanged){
                break;
            }
        }
        System.out.println("循环次数"+c);
    }


    @Override
    public void sort(int[] a) {
        int count = a.length-1;
        int temp;
        boolean exchanged = false;
        for(int i=count;i>0;i--){
            exchanged = false;
            for (int j=0;j<i;j++){
                if(a[j]>a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    exchanged = true;
                }
            }
            if(!exchanged){
                break;
            }
        }
    }
}
